#/usr/bin/python
# -*- coding:utf-8 -*-

'''
Process Windchill log files
Version 20151102
'''

__author__='zhaoxp'

import os
import re
import time
import datetime
import shutil
import logging
import zipfile

'''
Process all sorts of log files.
Input: 1, log file directory; 2, target log archive directory
Result: move log files(only file, not sub-directory) into directories which located in target directory and are named as 'year-month-day' format.
'''

def process_files_in_dir(logdir,archivedir,regexplist):
    assert os.path.isdir(logdir),logdir+' should be a directory'
    assert os.path.isdir(archivedir),archivedir+' should be a directory'
    filenamelist=os.listdir(logdir)
    dirset=set([])
    for filename in filenamelist:
        if matchExp(filename,regexplist) and os.path.isfile(os.path.join(logdir,filename)):
            #logging.info('Processing(process_files_in_dir) '+filename+' in '+logdir)
            process_a_file(os.path.join(logdir,filename),archivedir,dirset)
    for dir in dirset:
        zip_dir(dir)
        #start to remove directory that contains old log files
        logging.info("Remov dir "+dir)
        shutil.rmtree(dir)

def matchExp(filename,regexplist):
    for e in regexplist:
        if re.match(e,filename):
            return True
    return False

def process_a_file(filefullpath,archivedir,dirset):
    assert os.path.isdir(archivedir),archivedir+' should be a directory'
    #curtime=time.time()
    curtime=time.mktime(datetime.date.today().timetuple())
    filetime=os.path.getmtime(filefullpath)
    #archive log files which are older than 3 days (24*60*60*3)
    if curtime > (filetime+24*60*60*3):
        ftime=time.localtime(filetime)
        newdirfullpath=os.path.join(archivedir,str(ftime.tm_year)+'-'+str(ftime.tm_mon)+'-'+str(ftime.tm_mday))
        #logging.info('process_a_file : Processing file '+filefullpath+' ----> '+newdirfullpath)
        if not os.path.isdir(newdirfullpath):
            os.mkdir(newdirfullpath)
        assert os.path.isdir(newdirfullpath),newdirfullpath+' is not directory!'
        shutil.move(filefullpath,newdirfullpath)
        dirset.add(newdirfullpath)
        logging.info('Moving '+filefullpath+' ----> '+newdirfullpath)

def zip_dir(dirname):
    logging.info("Zip dir "+dirname)
    assert os.path.isdir(dirname),dirname+" should be a directory"
    filelist=[]
    for root,dirs,files in os.walk(dirname):
        for name in files:
            filelist.append(os.path.join(root,name))
    zf=zipfile.ZipFile(name_maker(dirname),"w",zipfile.zlib.DEFLATED,allowZip64=True)
    for tar in filelist:
        arcname=tar[(len(dirname)-len(os.path.basename(dirname))-1):]
        zf.write(tar,arcname)
    zf.close()

def name_maker(oldfullname):
    if not os.path.exists(oldfullname+'.zip'):
        return oldfullname+'.zip'
    i=2
    while(True):
        if not os.path.exists(oldfullname+'_'+str(i)+'.zip'):
            return oldfullname+'_'+str(i)+'.zip'
        i=i+1

if __name__=='__main__':
    logging.basicConfig(level=logging.INFO)
    logging.info('Processing Windchill log files')
    logconfigs=[]
    logconfigs.append(['/apphome/ptc/Windchill_10.0/Windchill/logs/',
        '/apphome/ptc/Windchill_10.0/Windchill/logs/oldlogs/',
        ['ServerManager.*','.*MethodServer.*']])
    logconfigs.append(['/apphome/ptc/Windchill_10.0/Windchill/logs/',
        '/apphome/ptc/Windchill_10.0/Windchill/logs/VKLog',
        ['VKLog_.*.csv']])
    logconfigs.append(['/apphome/ptc/Windchill_10.0/Windchill/logs/log4j',
        '/apphome/ptc/Windchill_10.0/Windchill/logs/log4j',
        ['ServerManager.*log4j.*','.*MethodServer.*log4j.*']])

    for conf in logconfigs:
        logging.info("Procssing log : "+str(conf))
        process_files_in_dir(conf[0],conf[1],conf[2])
    '''
    #single example:
    logdir='/apphome/ptc/Windchill_10.0/Windchill/logs/'
    archivedir='/apphome/ptc/Windchill_10.0/Windchill/logs/oldlogs/'
    regexplist=['dt_lenovo.tpgpre.*','ServerManager.*','BackgroundMethodServer.*']
    process_files_in_dir(logdir,archivedir,regexplist)
    '''
    logging.info('-------------------------end----------------------------')